package com.woniu.cinema.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.woniu.cinema.request.DynamicCinemaRequest;
import com.woniu.common.constant.Constants;
import org.bouncycastle.jcajce.provider.digest.MD2;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.woniu.cinema.mapper.CinemaMapper;
import com.woniu.cinema.domain.Cinema;
import com.woniu.cinema.service.CinemaService;

/**
 * 电影院信息Service业务层处理
 * 
 * @author yy
 * @date 2021-04-15
 */
@Service
public class CinemaServiceImpl implements CinemaService
{

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private CinemaMapper cinemaMapper;

    /**
     * 查询电影院信息
     * 
     * @param cinemaId 电影院信息ID
     * @return 电影院信息
     */
    @Override
    public Cinema selectCinemaById(Long cinemaId)
    {
        return cinemaMapper.selectCinemaById(cinemaId);
    }

    /**
     * 查询电影院信息列表
     * 
     * @param cinema 电影院信息
     * @return 电影院信息
     */
    @Override
    public List<Cinema> selectCinemaList(Cinema cinema)
    {
        return cinemaMapper.selectCinemaList(cinema);
    }

    /**
     * 新增电影院信息
     * 
     * @param cinema 电影院信息
     * @return 结果
     */
    @Override
    public int insertCinema(Cinema cinema)
    {
        return cinemaMapper.insertCinema(cinema);
    }

    /**
     * 修改电影院信息
     * 
     * @param cinema 电影院信息
     * @return 结果
     */
    @Override
    public int updateCinema(Cinema cinema)
    {
        cinemaMapper.updateCinema(cinema);
        Map<String,Long> msg = new HashMap<>();
        msg.put("id",cinema.getCinemaId());
        rabbitTemplate.convertAndSend(Constants.MQ_ES_EXCHANGE_NAME,Constants.MQ_ES_UPDATE_CINEMA_ROUTING_KEY,msg);
        return 1;
    }

    /**
     * 批量删除电影院信息
     * 
     * @param cinemaIds 需要删除的电影院信息ID
     * @return 结果
     */
    @Override
    public int deleteCinemaByIds(Long[] cinemaIds)
    {
        return cinemaMapper.deleteCinemaByIds(cinemaIds);
    }

    /**
     * 删除电影院信息信息
     * 
     * @param cinemaId 电影院信息ID
     * @return 结果
     */
    @Override
    public int deleteCinemaById(Long cinemaId)
    {
        return cinemaMapper.deleteCinemaById(cinemaId);
    }

    @Override
    public List<Cinema> selectCinemaListByDynamicFields(DynamicCinemaRequest request) {

        return cinemaMapper.selectCinemaListByDynamicFields( request);
    }

    @Override
    public List<Cinema> listAll(Long id) {
        if(id<0){
            return cinemaMapper.selectCinemaList(new Cinema());
        }else{
            return Arrays.asList(cinemaMapper.selectCinemaById(id));
        }

    }
}
